<style>
  .loginHome {
    display: inline !important;
  }
</style>
<style lang="less" scoped>
  .box-panel-header {
    padding: 15px 25px;
    border-bottom: 1px solid #eee;
    position: relative;
  }

  .b-p-h-title {
    line-height: 34px;
    font-size: 18px;
    display: inline-block;
    vertical-align: middle;
    color: #333;
  }

  .box-panel-body {
    padding: 25px 24px;
  }

  .flex-title {
    width: 100%;
    height: 50px;
    line-height: 25px;
    text-align: center;
  }

  .flex-body {
    width: 100%;
    display: flex;
    height: 60vh;
    margin-bottom: 20px;
    border: 1px solid #eee;
  }

  .flex-list {
    flex: 1;
    overflow: auto;
    padding-bottom: 5px;
    border-right: 1px solid #eee;
  }

  .flex-list-item {
    padding: 10px 10px;
    border-bottom: 1px solid #eee;
  }

  .flex-list-item:hover {
    background: #f5f5f5;
    cursor: pointer;
  }

  .flex-list-item>p {
    margin: 8px 0;
    font-size: 14px;
  }

  .f-l-i-title {
    font-size: 16px !important;
    font-weight: 700;
    color: #333;
  }

  .is-selected {
    background: #f5f5f5;
    cursor: pointer;
  }

  .g-l-i-title {
    padding-top: 25px;
    font-size: 18px;
    text-align: center;
  }

  .g-l-i-form {
    padding: 20px;
  }

  .g-l-m-title {
    padding-top: 25px;
    font-size: 18px;
    text-align: center;
  }

  .g-l-m-form {
    padding: 20px;
  }

  .flex-footer {
    text-align: center;
  }
</style>

<template>
  <div class="box-panel">
    <div class="box-panel-header">
      <span class="b-p-h-title">{{item_title}}</span>
    </div>
    <div class="box-panel-body">
      <div class="flex-title" v-if="isSelected > -1"><span
          style="font-size: 20px;font-weight: 700;color: #333;">{{isFormDataItem.name}}</span><span>/编辑</span></div>
      <div class="flex-title" v-if="isSelected < 0"><span
          style="font-size: 20px;font-weight: 700;color: #333;">新增分组</span></div>
      <div class="flex-body">
        <div class="group-list flex-list">
          <div>
            <div class="flex-list-item" v-for="(item,i) in formData" :key="i"
              :class="isSelected == i ? 'is-selected':''" @click="isItem(i)">
              <p class="f-l-i-title">{{item.name}}</p>
              <p class="f-l-i-desc" style="padding-left: 5px;">{{item.desc}}</p>
            </div>
          </div>
          <div class="flex-list-item" style="padding: 20px 10px; margin-top: 5px; border-top: 1px solid #eee;"
            @click="isItem(-1)">
            <div><span class="el-icon-plus"></span></div>
          </div>
        </div>
        <div class="group-list-info  flex-list">
          <div class="g-l-i-title">
            <span>分组信息</span>
          </div>
          <div class="g-l-i-form">
            <div>
              <p> <span style="color: red;">*</span> 分组名称：</p>
              <p>
                <el-input placeholder="请输入分组名称" v-model="isFormDataItem.name" clearable>
                </el-input>
              </p>
            </div>
            <div>
              <p> <span style="color: red;">*</span> 登陆跳转的首页(需要选择勾选菜单中的，否则会打不开页面)：</p>
              <p>
                <el-cascader class="loginHome" v-model="isFormDataItem.home_id" :options="menuListTree"
                  :props="loginHomeProps" :show-all-levels="false" clearable>
                </el-cascader>
              </p>
            </div>
            <div>
              <p>分组描述：</p>
              <p>
                <el-input placeholder="请输入分组描述" v-model="isFormDataItem.desc" clearable>
                </el-input>
              </p>
            </div>
          </div>
        </div>
        <div class="group-list-menu  flex-list">
          <div class="g-l-m-title">
            <span>分组显示菜单</span>
          </div>
          <div class="g-l-m-form">
            <div style="padding: 5px 0 10px 0;">
              <el-button size="mini" type="primary" @click="menu_resetAll()">全选</el-button>
              <el-button size="mini" type="info" @click="menu_resetChecked()">清空</el-button>
              <el-button size="mini" type="info" plain @click="menu_resetReset()">重置</el-button>
            </div>
            <el-tree :data="menuListTree" show-checkbox default-expand-all node-key="id" ref="menu_from_tree"
              highlight-current :props="defaultProps">
            </el-tree>
          </div>
        </div>
      </div>
      <div class="flex-footer">
        <div v-if="isSelected > -1">
          <el-button size="mini" type="primary" @click="GroupModify()">保存</el-button>
          <el-button size="mini" @click="Reset()">重置</el-button>
        </div>
        <div v-if="isSelected < 0">
          <el-button size="mini" type="primary" @click="AddGroup()">保存</el-button>
          <el-button size="mini" @click="Reset()">重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import $R from "../../js/common/request";
  import $P from "../../js/common/public";
  export default {
    data() {
      return {
        item_title: this.$route.meta.item_title,

        formData: [],
        isFormDataItem: {
          "id": "",
          "name": "",
          "desc": "",
          "home_id": "",
          "home": "",
          "menu": "",
        },
        isSelected: 0,

        menuListTree: [],
        menuListList: [],
        defaultProps: {
          children: 'children',
          label: 'title'
        },
        loginHome: "",
        loginHomeProps: {
          children: 'children',
          label: 'title',
          value: 'id',
          emitPath: false
        },
        options: [{
          value: 'zhinan',
          label: '指南',
        }, {
          value: 'zujian',
          label: '组件',
          children: [{
            value: 'basic',
            label: 'Basic',
            children: [{
              value: 'layout',
              label: 'Layout 布局'
            }, {
              value: 'color',
              label: 'Color 色彩'
            }, {
              value: 'typography',
              label: 'Typography 字体'
            }, {
              value: 'icon',
              label: 'Icon 图标'
            }, {
              value: 'button',
              label: 'Button 按钮'
            }]
          }, {
            value: 'form',
            label: 'Form',
            children: [{
              value: 'radio',
              label: 'Radio 单选框'
            }, {
              value: 'checkbox',
              label: 'Checkbox 多选框'
            }, {
              value: 'input',
              label: 'Input 输入框'
            }, {
              value: 'input-number',
              label: 'InputNumber 计数器'
            }, {
              value: 'select',
              label: 'Select 选择器'
            }, {
              value: 'cascader',
              label: 'Cascader 级联选择器'
            }, {
              value: 'switch',
              label: 'Switch 开关'
            }, {
              value: 'slider',
              label: 'Slider 滑块'
            }, {
              value: 'time-picker',
              label: 'TimePicker 时间选择器'
            }, {
              value: 'date-picker',
              label: 'DatePicker 日期选择器'
            }, {
              value: 'datetime-picker',
              label: 'DateTimePicker 日期时间选择器'
            }, {
              value: 'upload',
              label: 'Upload 上传'
            }, {
              value: 'rate',
              label: 'Rate 评分'
            }, {
              value: 'form',
              label: 'Form 表单'
            }]
          }, {
            value: 'data',
            label: 'Data',
            children: [{
              value: 'table',
              label: 'Table 表格'
            }, {
              value: 'tag',
              label: 'Tag 标签'
            }, {
              value: 'progress',
              label: 'Progress 进度条'
            }, {
              value: 'tree',
              label: 'Tree 树形控件'
            }, {
              value: 'pagination',
              label: 'Pagination 分页'
            }, {
              value: 'badge',
              label: 'Badge 标记'
            }]
          }, {
            value: 'notice',
            label: 'Notice',
            children: [{
              value: 'alert',
              label: 'Alert 警告'
            }, {
              value: 'loading',
              label: 'Loading 加载'
            }, {
              value: 'message',
              label: 'Message 消息提示'
            }, {
              value: 'message-box',
              label: 'MessageBox 弹框'
            }, {
              value: 'notification',
              label: 'Notification 通知'
            }]
          }, {
            value: 'navigation',
            label: 'Navigation',
            children: [{
              value: 'menu',
              label: 'NavMenu 导航菜单'
            }, {
              value: 'tabs',
              label: 'Tabs 标签页'
            }, {
              value: 'breadcrumb',
              label: 'Breadcrumb 面包屑'
            }, {
              value: 'dropdown',
              label: 'Dropdown 下拉菜单'
            }, {
              value: 'steps',
              label: 'Steps 步骤条'
            }]
          }, {
            value: 'others',
            label: 'Others',
            children: [{
              value: 'dialog',
              label: 'Dialog 对话框'
            }, {
              value: 'tooltip',
              label: 'Tooltip 文字提示'
            }, {
              value: 'popover',
              label: 'Popover 弹出框'
            }, {
              value: 'card',
              label: 'Card 卡片'
            }, {
              value: 'carousel',
              label: 'Carousel 走马灯'
            }, {
              value: 'collapse',
              label: 'Collapse 折叠面板'
            }]
          }]
        }, {
          value: 'ziyuan',
          label: '资源',
          children: [{
            value: 'axure',
            label: 'Axure Components'
          }, {
            value: 'sketch',
            label: 'Sketch Templates'
          }, {
            value: 'jiaohu',
            label: '组件交互文档'
          }]
        }],
      };
    },
    mounted() {
      $P.openLoad();
      this.init();
    },
    methods: {
      init() {
        let self = this;
        self.getDataList();
      },
      // 获取管理列表
      getDataList(type = "init") {
        if (type == "init") {
          let self = this;
          $R.Group.gList({
            success(res) {
              self.formData = res;
              self.getMenuList();
              self.isItem(self.isSelected);
            },
            error() {
              $P.closeLoad();
            }
          });
        } else {
          let self = this;
          $R.Group.gList({
            success(res) {
              self.formData = res;
              self.getMenuList();
              self.isItem(self.isSelected);
              setTimeout(function () {
                $P.closeLoad();
              }, "400");
            },
            error() {
              $P.closeLoad();
            }
          });
        }

      },
      // 获取菜单列表
      getMenuList() {
        let self = this;
        $R.Menu.menuList({
          success(res) {
            self.menuListTree = res.tree;
            self.menuListList = res.list;
            setTimeout(function () {
              self.menu_resetReset();
              $P.closeLoad();
            }, "400");
          },
          error() {
            $P.closeLoad();
          }
        });
      },
      // 获取菜单列表选中
      get_menu_reset() {
        let self = this;
        let tree = this.$refs.menu_from_tree;
        return tree.getCheckedKeys().concat(tree.getHalfCheckedKeys());
      },
      // 菜单选项全选
      menu_resetAll() {
        let self = this;
        self.$refs.menu_from_tree.setCheckedNodes(self.menuListTree);
      },
      // 菜单选项清空
      menu_resetChecked() {
        let self = this;
        setTimeout(function () {
            self.$refs.menu_from_tree.setCheckedKeys([]);
          }, "200");
      },
      // 菜单选项重置
      menu_resetReset() {
        let self = this;
        if (self.formData[self.isSelected]) {
          setTimeout(function () {
            self.$refs.menu_from_tree.setCheckedKeys(self.formData[self.isSelected]['menu'].split(','));
          }, "200");
        } else {
          self.menu_resetChecked();
        }
      },
      // 切换选项
      isItem(i) {
        let self = this;
        self.isSelected = i;
        if (i > -1) {
          self.isFormDataItem['id'] = self.formData[i]['id'];
          self.isFormDataItem['name'] = self.formData[i]['name'];
          self.isFormDataItem['desc'] = self.formData[i]['desc'];
          self.isFormDataItem['home_id'] = self.formData[i]['home_id'];
          self.isFormDataItem['home'] = self.formData[i]['home'];
          self.isFormDataItem['menu'] = self.formData[i]['menu'];

        } else {
          self.isFormDataItem = {
            "id": "",
            "name": "",
            "desc": "",
            "home_id": "",
            "home": "",
            "menu": "",
          };
        }
        self.menu_resetReset();
      },
      // 修改分组
      GroupModify() {
        let self = this;
        $P.openLoad();
        if ($P.Invalid(self.isFormDataItem['id'])) {
          $P.Msg("请选择需要修改的分组！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }

        if ($P.Invalid(self.isFormDataItem['name'])) {
          $P.Msg("请填写分组名称！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }

        if ($P.Invalid(self.isFormDataItem['home_id'])) {
          $P.Msg("请选择跳转的首页！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }
        self.menuListList.forEach(element => {
          if (element.id == self.isFormDataItem['home_id']) {
            self.isFormDataItem['home'] = element.key;
          }
        });

        if ($P.Invalid(self.isFormDataItem['home'])) {
          $P.Msg("选择跳转的首页出错，请刷新重新选择！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }

        let menu = self.get_menu_reset();
        if (menu.length < 1) {
          $P.Msg("请选择分组显示菜单！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }
        self.isFormDataItem['menu'] = menu.join(',');
        $R.Group.gModify({
          params: Object.assign({}, self.isFormDataItem),
          success(res) {
            $P.Msg("修改成功")
            self.getDataList("res");
          },
          error() {
            $P.closeLoad();
          }
        });
      },
      // 编辑重置
      Reset() {
        this.isItem(this.isSelected);
      },
      // 添加分组
      AddGroup() {
        let self = this;
        $P.openLoad();

        if ($P.Invalid(self.isFormDataItem['name'])) {
          $P.Msg("请填写分组名称！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }

        if ($P.Invalid(self.isFormDataItem['home_id'])) {
          $P.Msg("请选择跳转的首页！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }
        self.menuListList.forEach(element => {
          if (element.id == self.isFormDataItem['home_id']) {
            self.isFormDataItem['home'] = element.key;
          }
        });

        if ($P.Invalid(self.isFormDataItem['home'])) {
          $P.Msg("选择跳转的首页出错，请刷新重新选择！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }

        let menu = self.get_menu_reset();
        if (menu.length < 1) {
          $P.Msg("请选择分组显示菜单！", 4);
          setTimeout(function () {
            $P.closeLoad();
          }, "400");
          return;
        }
        self.isFormDataItem['menu'] = menu.join(',');

        $R.Group.gAdd({
          params: Object.assign({}, self.isFormDataItem),
          success(res) {
            $P.Msg("添加成功")
            self.isSelected = 0;
            self.getDataList();
          },
          error() {
            $P.closeLoad();
          }
        });

      },
    },
    computed: {}
  };
</script>